
/* Change Theme */
function changeTheme(themeName){
    var $easyuiTheme = $('#easyuiTheme');
    var url=$easyuiTheme.attr('href');
    var href=url.substring(0, url.indexOf('themes')) + 'themes/' + themeName + '/easyui.css';
    $easyuiTheme.attr('href',href);

    var $iframe = $('iframe');
    if($iframe.length > 0){
        
        for(var i=0;i<$iframe.length;i++){
            var ifr = $iframe[i];
            $(ifr).contents().find('#easyuiTheme').attr('href',href);
        }
    }
    $.cookie('easyuiThemeName',themeName,{
        expires: 7
    });
}


/* Add Tab */
function addTab(title, url, icon ,parent_){
    if(parent_){
         var centerpanel = parent.$('#center-panel');
    }else{
        var centerpanel = $('#center-panel');
    }
    

    if (centerpanel.tabs('exists', title)){
        centerpanel.tabs('select', title);
    } else {
        var contents = '<iframe id="jiao" scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;" ></iframe>';

        centerpanel.tabs('add',{
            title:title,
            iconCls:icon,
            content:contents,
            closable:true
        });
    }
    changeTheme($.cookie('easyuiThemeName'));
}



//刷新首页的选项卡
function addIndexTab(title, url, icon){
    var centerpanel = $('#center-panel');

    if (centerpanel.tabs('exists', title)){
        centerpanel.tabs('select', title);
        var tab = centerpanel.tabs('getSelected');
        tab.panel('refresh', url);
    } else {
        var contents = '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
        
        centerpanel.tabs('add',{
            title:title,
            iconCls:icon,
            content:contents,
            closable:true
        });
    }

}

/* Add Patient Tab */
function addPatientTab(title, url, icon){
    var maintab = $('#main-tab');

    if (maintab.tabs('exists', title)){
        maintab.tabs('select', title);
        var tab = maintab.tabs('getSelected');
        tab.panel('refresh', url);
    } else {
        var contents = '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';

        maintab.tabs('add',{
            title:title,
            iconCls:icon,
            content:contents,
            closable:true
        });
        var tabnew = maintab.tabs('getSelected');
        tabnew.panel('refresh', url);
    }

}

/* calculate age by dob */
function ages(str) {
    if(undefined !== str && null !== str){
        var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
        if(r==null)
            return false;
        var d = new Date(r[1], r[3]-1, r[4]);
        if(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]){
            var Y = new Date().getFullYear();
            return((Y-r[1])+"周岁");
        }
    }else
        return null;
}
